package com.example.interfaces.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import com.example.infrastructure.common.Result;
import com.example.infrastructure.common.PageResult;
import com.example.infrastructure.excel.ExcelUtils;
import com.example.infrastructure.lock.DistributedLockUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;
import com.example.application.service.ConsultationsService;
import com.example.infrastructure.persistence.po.Consultations;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 问诊记录表 控制器
 *
 * @author wangal
 * @since 2025-07-25
 */
@RestController
@RequestMapping("/consultations")
@Tag(name = "问诊记录表管理", description = "问诊记录表相关接口")
@Validated
public class ConsultationsController {

    @Autowired
    private ConsultationsService consultationsService;
    
    @Autowired
    private DistributedLockUtils distributedLockUtils;

    /**
     * 创建问诊记录表
     */
    @PostMapping
    @Operation(summary = "创建问诊记录表")
    public Result<Boolean> create(@Valid @RequestBody Consultations consultations) {
        return Result.success(consultationsService.save(consultations));
    }

    /**
     * 根据ID删除问诊记录表
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除问诊记录表")
    public Result<Boolean> delete(@Parameter(description = "主键ID") @PathVariable @NotNull Long id) {
        return Result.success(consultationsService.removeById(id));
    }

    /**
     * 批量删除问诊记录表
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除问诊记录表")
    public Result<Boolean> deleteBatch(@RequestBody List<Long> ids) {
        return Result.success(consultationsService.removeByIds(ids));
    }

    /**
     * 更新问诊记录表
     */
    @PutMapping
    @Operation(summary = "更新问诊记录表")
    public Result<Boolean> update(@Valid @RequestBody Consultations consultations) {
        return Result.success(consultationsService.updateById(consultations));
    }

    /**
     * 根据ID查询问诊记录表
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询问诊记录表详情")
    public Result<Consultations> getById(@Parameter(description = "主键ID") @PathVariable @NotNull Long id) {
        return Result.success(consultationsService.getById(id));
    }

    /**
     * 分页查询问诊记录表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询问诊记录表")
    public Result<PageResult<Consultations>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Long size) {
        Page<Consultations> page = new Page<>(current, size);
        Page<Consultations> result = consultationsService.page(page);
        return Result.success(PageResult.of(result));
    }

    /**
     * 查询所有问诊记录表
     */
    @GetMapping("/list")
    @Operation(summary = "查询所有问诊记录表")
    public Result<List<Consultations>> list() {
        return Result.success(consultationsService.list());
    }

    /**
     * 导出问诊记录表
     */
    @GetMapping("/export")
    @Operation(summary = "导出问诊记录表")
    public void export(HttpServletResponse response) {
        List<Consultations> list = consultationsService.list();
        ExcelUtils.export(response, "问诊记录表数据", "问诊记录表", Consultations.class, list);
    }

    /**
     * 导入问诊记录表
     */
    @PostMapping("/import")
    @Operation(summary = "导入问诊记录表")
    public Result<String> importData(@RequestParam("file") MultipartFile file) {
        return distributedLockUtils.executeWithLock("import:consultations", () -> {
            List<Consultations> list = ExcelUtils.importExcel(file, Consultations.class, null);
            boolean success = consultationsService.saveBatch(list);
            return success ? Result.success("导入成功，共" + list.size() + "条数据") 
                          : Result.error("导入失败");
        });
    }

    /**
     * 下载导入模板
     */
    @GetMapping("/template")
    @Operation(summary = "下载导入模板")
    public void downloadTemplate(HttpServletResponse response) {
        List<Consultations> templateData = List.of(new Consultations());
        ExcelUtils.export(response, "问诊记录表导入模板", "问诊记录表", Consultations.class, templateData);
    }
}
